package com.jd.cs.config.interceptor;

import com.jd.cs.common.Const;
import com.jd.cs.common.ThreadLocalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: lizhiwei
 * @date: 2020-07-25 20:23
 **/
@Component
public class LangInterceptor extends HandlerInterceptorAdapter {
    private final static Logger logger = LoggerFactory.getLogger(LangInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        processLang(request);
        return true;
    }

    /**
     * 处理语言
     *
     * @param request
     */
    private void processLang(HttpServletRequest request) {
        String lang = request.getHeader(Const.Header.ACCEPT_LANGUAGE);
        //如果没有包含语言，默认使用中文
        if (StringUtils.isEmpty(lang)) {
            lang = Const.Lang.ZH_CN;
        }
        ThreadLocalUtil.set(Const.Lang.LANG, lang);
    }

}